//
//  HomeViewController.m
//  GCD
//
//  Created by fangyuan ding on 2020/8/5.
//  Copyright © 2020 fangyuan ding. All rights reserved.
//

#import "GCDViewController.h"

@interface GCDViewController ()

@property (nonatomic, strong)UIScrollView *scrollView;

@property (nonatomic, strong)UIImageView *imageView;


@property (nonatomic, strong)NSMutableArray *photoList;

@end

@implementation GCDViewController
- (NSMutableArray *)photoList {
    if(!_photoList){
        _photoList = [NSMutableArray array];
    }
    return _photoList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self test1];
//    [self test2];
//    [self test3];
//    [self test4];
//    [self test5];
//      [self test6];
    [self test7];
    
    
    
    
}

- (void)test1{
    //使用GCD同步若干异步调用，根据若干个URL异步加载多张图片，然后下载完成后合成一张图
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        NSLog(@"加载图片1");
        //加载图片1
    });
    dispatch_group_async(group, queue, ^{
         NSLog(@"加载图片2");
        //加载图片2
    });
    dispatch_group_async(group, queue, ^{
         NSLog(@"加载图片3");
        //加载图片3
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"合并图片");
        //合并图片
    });
}
- (void)test2{
    self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    self.imageView = [[UIImageView alloc]init];
    [self.scrollView addSubview:self.imageView];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [NSURL URLWithString:@"https://pics4.baidu.com/feed/359b033b5bb5c9ea218a766c2dc95d053bf3b33e.jpeg?token=e5d87fc8ccc5bf28ace671b69129f577&s=DC8201D79D539AD2467924A703005001"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *imge = [UIImage imageWithData:data];
        //到主线程更新UI
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.imageView.image = imge;
            [self.imageView sizeToFit];
            self.scrollView.contentSize = imge.size;
        });
    });
}
- (void)timerTest{
    NSLog(@"timer %@",[NSRunLoop currentRunLoop].currentMode);
    //开启子线程的消息循环
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
//    [[NSRunLoop  currentRunLoop] run];
    NSLog(@"end");
    
}
// //在子线程的消息循环中
- (void)threadTest{
    NSLog(@"thread %@",[NSRunLoop currentRunLoop].currentMode);
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//
////    [self performSelector:@selector(test) withObject:NULL];
////    //创建队列
////    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
////    //创建任务
////    dispatch_block_t task = ^{
////        NSLog(@"hello %@",[NSThread currentThread]);
////    };
////    dispatch_async(queue, task);
//
////    dispatch_async(dispatch_get_global_queue(0, 0), ^{
////          NSLog(@"hello %@",[NSThread currentThread]);
////    });
//
//}

- (void)test{
    __block int a = 0;
    while (a < 5) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            a++;
        });
        NSLog(@"里面a == %d",a);
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"外面a == %d",a);
    });
}


- (void)test3{
      //串行对列的同步执行
//        [self demo1];
        //串行队列的异步执行
//        [self demo2];
        //并行队列的同步执行
//        [self demo3];
        ////并行队列的异步执行
//        [self demo4];
        //主队列 同步执行
//        [self demo5];
        //主队列 异步执行
        [self demo6];
}
//串行对列的同步执行 不开启线程 任务是按顺序进行

- (void)demo1{
    dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_SERIAL);
    for(int  i = 0; i < 10 ; i ++){
        dispatch_sync(queue, ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
   
}

//串行队列的异步执行  开启新线程 任务有序执行
- (void)demo2{
     dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_SERIAL);
    for(int  i = 0; i < 10 ; i ++){
        dispatch_async(queue, ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
}

//并行队列的同步执行  ===    串行对列的同步执行 不开启线程 任务是按顺序进行
- (void)demo3{
    dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_CONCURRENT);
    for(int  i = 0; i < 10 ; i ++){
        dispatch_sync(queue, ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
}
//并行队列的异步执行 开多个线程  无序执行
- (void)demo4{
    dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_CONCURRENT);
    for(int  i = 0; i < 10 ; i ++){
        dispatch_async(queue, ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
}
//主队列 同步执行  死锁
//同步执行：会等着第一个任务执行完成，才会继续向后执行
- (void)demo5{
    for(int i = 0 ; i < 10 ; i++){
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
}

//主队列 异步执行  主线程 顺序执行
//主队列的特点：先执行完主线程上的代码，才会执行主队列中的任务
- (void)demo6{
    for(int i = 0 ; i < 10 ; i++){
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
    
}
//全局队列
- (void)demo7{
   
    //iOS7以后推荐使用服务质量
    for(int i = 0 ; i < 10 ; i++){
        dispatch_async( dispatch_get_global_queue(0, 0), ^{
            NSLog(@"hello %d %@",i,[NSThread currentThread]);
        });
    }
    
}

- (void)test4{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           NSLog(@"task");
       });
       //一次性执行
       static dispatch_once_t onceToken;
       dispatch_once(&onceToken, ^{
            NSLog(@"%@", [NSThread currentThread]);
       });
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //一次性执行
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"%@", [NSThread currentThread]);
    });
}
- (void)downLoadImage:(int)index{
    dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSString *fileName = @"2.png";
        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
        UIImage *img = [UIImage imageWithContentsOfFile:path];
        dispatch_barrier_async(queue, ^{
            [self.photoList addObject:img];
            NSLog(@"保存图片%d %@",index,[NSThread currentThread]);
        });
        NSLog(@"下载完成%d %@",index,[NSThread currentThread]);
    });
    
}
- (void)test5{
    //异步 子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"输入密码%@",[NSThread currentThread]);
        });
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"扣费%@",[NSThread currentThread]);
            [NSThread sleepForTimeInterval:1];
        });
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"下载应用%@",[NSThread currentThread]);
        });
        
    });
}
- (void)test6{
    dispatch_queue_t serialQueue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
    NSLog(@"1");
    dispatch_async(serialQueue, ^{
        NSLog(@"2");
    });
    NSLog(@"3");
    dispatch_sync(serialQueue, ^{
        NSLog(@"4");
    });
    NSLog(@"5");
    
}
- (void)test7{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    for(NSInteger i = 0; i < 10; i++){
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"%zd",i);
        });
    }
    dispatch_barrier_sync(concurrentQueue, ^{
        NSLog(@"barrier");
        
    });
    for(NSInteger i = 10; i < 20; i++){
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"%zd",i);
        });
    }
    
}
- (void)test8{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    for(NSInteger i = 0; i < 10;i++){
        dispatch_group_async(group, concurrentQueue, ^{
            sleep(1);
            
        });
        
    }
}
@end
